<template>
  <Container class="gift-sale">
    <!-- 基础设置 -->
    <div class="container-header title title-center" slot="header">
      赠品促销
      <Button
        size="large"
        class="fl btn-back"
        icon="ios-arrow-back"
        @click="jumpPrevPage"
      >返回</Button>
    </div>
    <DefaultEditItem ref="giftSaleForm" :form="$store.state.giftSale.Giftsaleform"></DefaultEditItem>
    <div class="container-footer bg-white" slot="footer">
      <Button type="primary" size="large" class="ml" @click="onShowList">下一步</Button>
    </div>
  </Container>
</template>
<script>
import DefaultEditItem from "./edit/DefaultEditItem";
export default {
  name: "",
  components: {
    DefaultEditItem
  },
  data() {
    return {
      id: this.$route.query.id || '',
      type: this.$route.query.type || '',
      isEmpty: false, // 是否清空数据
      isSave: true // 是否保存数据
    }
  },
  methods: {
    getDetailData() {
      this.$ajaxGet('/activity/gift/detail/' + `${this.id}`).then(data => {
          this.$store.state.giftSale.Giftsaleform.remarks = data.activityInfoDTO.activityDescription //活动备注
          this.$store.state.giftSale.Giftsaleform.name = data.activityInfoDTO.activityName //促销名称
          this.$store.state.giftSale.Giftsaleform.grade = this.promotionMemberScope = data.activityInfoDTO.storeTypeEnumList, //活动对象
          this.$store.state.giftSale.Giftsaleform.date = data.activityGiftDTO.giftTimeList.map(item=> {
            return {
              value: [new Date(item.startDatetime), new Date(item.endDatetime)],
              index: 1, 
              status: 1
            }
          })
          this.$store.state.giftSale.Giftsaleform.least = data.activityGiftDTO.minQuantityPerBuy
      })
    },
    jumpPrevPage() {
      this.$router.back()
    },
    onShowList() {
      let self = this
      this.$refs["giftSaleForm"].$refs["form"].validate(valid => {
        if (valid) {
          let url = {name: 'sales/gift/list', query: {}}
          this.$Message.success("保存成功!")
          if (self.id) {
            url.query.id = self.id
            if(self.type) url.query.type = self.type
          }
          this.$router.push(url)
        }
      })
    }
  },
  beforeRouteEnter(to, from, next) {
      /*
        1. 路由从不是下一页进入的都清空
        2. 路由从下一页进入并且有id就不刷新接口--保存信息
      */
      next(vm=> {
        if (from.name !== 'sales/gift/list') {
          vm.isEmpty = true
        } else {
          vm.isSave = false
        }
        if (vm.isEmpty) vm.$store.commit('setGiftSaleEmpty')
        if (vm.id && vm.isSave) vm.getDetailData()
      })
  }
};
</script>

